/*
 * Copyright (c) 2015, 张涛.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.kymjs.themvp.view;

import ohos.aafwk.ability.Ability;
import ohos.agp.components.Component;
import ohos.agp.components.ComponentContainer;
import ohos.agp.components.LayoutScatter;
import ohos.agp.window.dialog.ToastDialog;
import ohos.app.Context;

import java.util.HashMap;

/**
 * View delegate base class
 * 视图层代理的基类
 *
 * @author kymjs (http://www.kymjs.com/) on 10/23/15.
 */
public abstract class AppDelegate implements IDelegate {
    /**
     * views
     */
    protected final HashMap<Integer, Component> mViews = new HashMap<>();
    /**
     * rootView
     */
    protected ComponentContainer rootView;

    /**
     * getRootLayoutId
     *
     * @return layoutId
     */
    public abstract int getRootLayoutId();

    /**
     * create
     *
     * @param context      context
     * @param root         root
     * @param attachToRoot attachToRoot
     */
    @Override
    public void create(Context context, ComponentContainer root, boolean attachToRoot) {
        int rootLayoutId = getRootLayoutId();
        rootView = (ComponentContainer) LayoutScatter.getInstance(context).parse(rootLayoutId, root, attachToRoot);
    }

    /**
     * getOptionsMenuId
     *
     * @return id
     */
    @Override
    public int getOptionsMenuId() {
        return 0;
    }


    /**
     * getRootView
     *
     * @return ComponentContainer
     */
    @Override
    public ComponentContainer getRootView() {
        return rootView;
    }

    /**
     * setRootView
     *
     * @param rootView rootView
     */
    public void setRootView(ComponentContainer rootView) {
        this.rootView = rootView;
    }

    /**
     * initWidget
     */
    @Override
    public void initWidget() {
    }

    /**
     * bindView
     *
     * @param id  id
     * @param <T> component
     * @return component
     */
    public <T extends Component> T bindView(int id) {
        T view = (T) mViews.get(id);
        if (view == null) {
            view = (T) rootView.findComponentById(id);
            mViews.put(id, view);
        }
        return view;
    }

    /**
     * get Component
     *
     * @param id  id
     * @param <T> component
     * @return getComponent
     */
    public <T extends Component> T get(int id) {
        return (T) bindView(id);
    }

    /**
     * setOnClickListener
     *
     * @param listener listener
     * @param ids      ids
     */
    public void setOnClickListener(Component.ClickedListener listener, int... ids) {
        if (ids == null) {
            return;
        }
        for (int id : ids) {
            get(id).setClickedListener(listener);
        }
    }

    /**
     * toast
     *
     * @param msg msg
     */
    public void toast(CharSequence msg) {
        new ToastDialog(rootView.getContext()).setText(msg.toString()).show();
    }

    /**
     * getActivity
     *
     * @param <T> ability
     * @return ability
     */
    public <T extends Ability> T getActivity() {
        return (T) rootView.getContext();
    }
}
